package com.ilibrary.web.action;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import com.ilibrary.base.MyAjaxAction;
import com.ilibrary.base.MyBaseVO;
import com.ilibrary.constants.MyConstants;
import com.ilibrary.constants.MyMsgConstants;
import com.ilibrary.service.BorrowService;
import com.ilibrary.utils.MyTools;
import com.ilibrary.web.dto.ResultDto;
import com.ilibrary.web.vo.AdminVO;
import com.ilibrary.web.vo.BorrowVO;

public class DoAddBorrowAction extends MyAjaxAction {

	private BorrowService borrowService;
	
	public BorrowService getBorrowService() {
		return borrowService;
	}
	public void setBorrowService(BorrowService borrowService) {
		this.borrowService = borrowService;
	}

	@Override
	protected JSONObject doExecute(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception {
		String bookId = request.getParameter("bookId");
		String userId = request.getParameter("userId");
		JSONObject jo = new JSONObject();
		
		AdminVO adminVO = (AdminVO) session.getAttribute(MyConstants.ILIBRARY_LOGIN_ADMIN);
		
		BorrowVO borrowVO = new BorrowVO();
		borrowVO.setBorrowId(0);
		borrowVO.setBookId(Integer.valueOf(bookId));
		borrowVO.setUserId(userId);
		Date date = new Date();
		borrowVO.setBorrowDt(date);
		borrowVO.setBorrowDeadline(MyTools.getNextXDayDate(date, MyConstants.BOOK_LOAN_DAY_MAX));
		borrowVO.setCreateDt(date);
		borrowVO.setUpdateDt(date);
		borrowVO.setBorrowRenewNum(0);
		borrowVO.setCreateUser(adminVO.getAdminId());
		borrowVO.setUpdateUser(adminVO.getAdminId());
		
		ResultDto<MyBaseVO> resultDto = borrowService.add(borrowVO);
		if(!resultDto.isSuccess()) {
			jo.put("success", false);
			jo.put("msg", resultDto.getMsg());
			return jo;
		}
		jo.put("success", true);
		return jo;
	}

}
